* May 23, 2025
* Duffy, Lebeau, Puzzello

* This file replicates the results found in Tables 1, 2 (data rows), 4, 5 and 6 in the main text; D1, D2, D3 and D4 in the Online Appendix.
* It also includes the Kolmogorov - Smirnov tests and Barrett-Donald tests reported in the text.

* Important: the "Data import and variable creation" block must be run first (after setting the directory). Other blocks can then be run independently. 

*****************************************
*** Data import and variable creation ***
*****************************************

* Set directory HERE
* cd "c:\user|lenovo\desktop\replication_DLP\utility" \\ Example
cd "c:\...\replication_DLP\utility"

use final_bids.dta 
* This .dta file contains two rows for each pair of players for each round of negotiation, corresponding to the final outcome of the negotiation (once corresponding to the seller, once corresponding to the buyer) 

gen buyersurplus = 74.1752*(quantity^0.6) - payment
gen sellersurplus = payment - 8.23249*(quantity^1.51678)
gen totalsurplus = buyersurplus + sellersurplus
gen bratio = buyersurplus / totalsurplus

gen firsthalf=1 if round <=15
replace firsthalf=0 if missing(firsthalf)
gen secondhalf=abs(1-firsthalf)

gen price=payment/quantity

gen m30=1 if etokens==30
replace m30=0 if missing(m30)
gen m60=1 if etokens==60
replace m60=0 if missing(m60)
gen m315=1 if etokens==315
replace m315=0 if missing(m315)

gen qstarclose05=1 if etokens==315 & abs(quantity-4)<=0.5
replace qstarclose05=0 if missing(qstarclose05)
gen qstarclose01=1 if etokens==315 & abs(quantity-4)<=0.1
replace qstarclose01=0 if missing(qstarclose01)
gen qstarclose005=1 if etokens==315 & abs(quantity-4)<=0.05
replace qstarclose005=0 if missing(qstarclose005)

gen constrained=1 if m30==1|m60==1
replace constrained=0 if missing(constrained)
gen ystarclose05=1 if constrained==1 & abs(payment-etokens)<=0.5
replace ystarclose05=0 if missing(ystarclose05)
gen ystarclose01=1 if constrained==1 & abs(payment-etokens)<=0.1
replace ystarclose01=0 if missing(ystarclose01)
gen ystarclose005=1 if constrained==1 & abs(payment-etokens)<=0.05
replace ystarclose005=0 if missing(ystarclose005)

gen prob=0.5 if treathalf==1
replace prob=0.25 if treatquarter==1

gen uniqueid = session * 10 + participantid_in_session - 10

***************
*** Table 1 ***
***************

preserve
keep if session <=15
xtreg bratio if etokens==315 & role==2, i(uniqueid) re vce(cluster session) 
xtreg bratio if etokens==315 & role==2 & qstarclose05==1, i(uniqueid) re vce(cluster session) 
xtreg bratio if etokens==315 & role==2 & qstarclose01==1, i(uniqueid) re vce(cluster session) 
xtreg bratio if etokens==315 & role==2 & qstarclose005==1, i(uniqueid) re vce(cluster session) 
restore

***************************
*** Table 2 (data rows) ***
***************************

preserve
keep if session <= 15
sum quantity payment price sellersurplus buyersurplus totalsurplus bratio if etokens==315 & agreement==1 & role==1
sum quantity payment price sellersurplus buyersurplus totalsurplus bratio if etokens==60 & agreement==1 & role==1
sum quantity payment price sellersurplus buyersurplus totalsurplus bratio if etokens==30 & agreement==1 & role==1
restore

*******************************************************
*** Kolmogorov - Smirnov  and Barrett- Donald Tests ***
*******************************************************

preserve
keep if session <= 15
gen dummy31560=1 if etokens==315
replace dummy31560=0 if etokens==60
gen dummy6030=1 if etokens==60
replace dummy6030=0 if etokens==30

ksmirnov quantity if agreement==1 & role==1, by(dummy31560)
ksmirnov quantity if agreement==1 & role==1, by(dummy6030)
ksmirnov payment if agreement==1 & role==1, by(dummy31560)
ksmirnov payment if agreement==1 & role==1, by(dummy6030)
ksmirnov sellersurplus if agreement==1 & role==1, by(dummy31560)
ksmirnov sellersurplus if agreement==1 & role==1, by(dummy6030)
ksmirnov totalsurplus if agreement==1 & role==1, by(dummy31560)
ksmirnov totalsurplus if agreement==1 & role==1, by(dummy6030)
ksmirnov buyersurplus if agreement==1 & role==1, by(dummy31560)
ksmirnov buyersurplus if agreement==1 & role==1, by(dummy6030)
ksmirnov bratio if agreement==1 & role==1, by(dummy31560)
ksmirnov bratio if agreement==1 & role==1, by(dummy6030)

* If needed, install package by uncommenting and running command below
*  net install pysdtest, from("https://raw.githubusercontent.com/lee-kyungho/pysdtest/main/Stata") replace
pysdtest quantity if agreement==1 & role==1, by(dummy31560) 	
pysdtest quantity if agreement==1 & role==1, by(dummy6030)
pysdtest tokens if agreement==1 & role==1, by(dummy31560) 
pysdtest tokens if agreement==1 & role==1, by(dummy6030) 
pysdtest sellersurplus if agreement==1 & role==1, by(dummy31560) 
pysdtest sellersurplus if agreement==1 & role==1, by(dummy6030) 
pysdtest totalsurplus if agreement==1 & role==1, by(dummy31560)
pysdtest totalsurplus if agreement==1 & role==1, by(dummy6030) 
pysdtest buyersurplus if agreement==1 & role==1, by(dummy31560)
pysdtest buyersurplus if agreement==1 & role==1, by(dummy6030) 
pysdtest bratio if agreement==1 & role==1, by(dummy31560) 
pysdtest bratio if agreement==1 & role==1, by(dummy6030) 
pysdtest quantity if agreement==1 & role==1, by(dummy31560) switch	
pysdtest quantity if agreement==1 & role==1, by(dummy6030) switch
pysdtest tokens if agreement==1 & role==1, by(dummy31560) switch
pysdtest tokens if agreement==1 & role==1, by(dummy6030) switch
pysdtest sellersurplus if agreement==1 & role==1, by(dummy31560) switch
pysdtest sellersurplus if agreement==1 & role==1, by(dummy6030) switch
pysdtest totalsurplus if agreement==1 & role==1, by(dummy31560) switch
pysdtest totalsurplus if agreement==1 & role==1, by(dummy6030) switch
pysdtest buyersurplus if agreement==1 & role==1, by(dummy31560) switch
pysdtest buyersurplus if agreement==1 & role==1, by(dummy6030) switch
pysdtest bratio if agreement==1 & role==1, by(dummy31560) switch
pysdtest bratio if agreement==1 & role==1, by(dummy6030) switch
restore

***************
*** Table 4 ***
***************

preserve
keep if session <= 15
xtreg buyersurplus m30 m315 if role==2 & agreement==1, i(uniqueid) re vce(cluster session)  
xtreg buyersurplus m30 m315 if role==2 & agreement==1 & (qstarclose05==1 | ystarclose05==1), i(uniqueid) re vce(cluster session)  
restore

***************
*** Table 5 ***
***************

preserve
keep if session > 15
sort prob
by prob: summarize (quantity payment price sellersurplus buyersurplus totalsurplus bratio) if agreement==1 & role==1

* Agreement rates
by prob: summarize agreement if role==1
by prob: summarize buyeraccepted if agreement==1
restore

***************
*** Table 6 ***
***************

preserve
keep if session > 15
xtreg bratio if etokens==315 & role==2 & treatquarter==1, i(uniqueid) re vce(cluster session)  
xtreg bratio if etokens==315 & role==2 & qstarclose05==1 & treatquarter==1, i(uniqueid) re vce(cluster session) 
xtreg bratio if etokens==315 & role==2 & treathalf==1, i(uniqueid) re vce(cluster session)  
xtreg bratio if etokens==315 & role==2 & qstarclose05==1 & treathalf==1, i(uniqueid) re vce(cluster session) 
restore

*****************************
*** Table 7 (last column) ***
*****************************

preserve
replace prob=1 if missing(prob)
xtreg bratio if etokens==315 & role==2 & prob==1, i(uniqueid) re vce(cluster session)  
xtreg bratio if etokens==60 & role==2 & prob==1, i(uniqueid) re vce(cluster session)  
xtreg bratio if etokens==30 & role==2 & prob==1, i(uniqueid) re vce(cluster session)  
xtreg bratio if etokens==315 & role==2 & treatquarter==1, i(uniqueid) re vce(cluster session)
xtreg bratio if etokens==315 & role==2 & treathalf==1, i(uniqueid) re vce(cluster session)  
restore

*************************
*** Appendix Table D1 ***
*************************

preserve
keep if session <= 15
tab etokens, sum(agreement)
tab etokens session if firsthalf==1, sum(agreement) 
tab etokens session if secondhalf==1, sum(agreement) 
restore

*************************
*** Appendix Table D2 ***
*************************

preserve
keep if session <= 15
tab session secondhalf if etokens==30 & agreement==1, sum(quantity)
tab session secondhalf if etokens==30 & agreement==1, sum(payment)
tab session secondhalf if etokens==30 & agreement==1, sum(buyersurplus)
tab session secondhalf if etokens==30 & agreement==1, sum(totalsurplus)
tab session secondhalf if etokens==30 & agreement==1, sum(bratio)
tab session secondhalf if etokens==60 & agreement==1, sum(quantity)
tab session secondhalf if etokens==60 & agreement==1, sum(payment)
tab session secondhalf if etokens==60 & agreement==1, sum(buyersurplus)
tab session secondhalf if etokens==60 & agreement==1, sum(totalsurplus)
tab session secondhalf if etokens==60 & agreement==1, sum(bratio)
tab session secondhalf if etokens==315 & agreement==1, sum(quantity)
tab session secondhalf if etokens==315 & agreement==1, sum(payment)
tab session secondhalf if etokens==315 & agreement==1, sum(buyersurplus)
tab session secondhalf if etokens==315 & agreement==1, sum(totalsurplus)
tab session secondhalf if etokens==315 & agreement==1, sum(bratio)
restore

*************************
*** Appendix Table D3 ***
*************************

preserve
keep if session <= 15
xtreg bratio if etokens==315 & role==1, i(uniqueid) re vce(cluster session) 
xtreg bratio if etokens==315 & role==1 & qstarclose05==1, i(uniqueid) re vce(cluster session) 
xtreg bratio if etokens==315 & role==1 & qstarclose01==1, i(uniqueid) re vce(cluster session) 
xtreg bratio if etokens==315 & role==1 & qstarclose005==1, i(uniqueid) re vce(cluster session) 
restore

*************************
*** Appendix Table D4 ***
*************************

preserve
keep if session <= 15
xtreg buyersurplus m30 m315 if role==1 & agreement==1, i(uniqueid) re vce(cluster session)  
xtreg buyersurplus m30 m315 if role==1 & agreement==1 & (qstarclose05==1 | ystarclose05==1), i(uniqueid) re vce(cluster session)  
restore

*****************
*** Figure 10 ***
*****************

preserve
keep if session <= 15
drop if agreement==0
drop if role==1
gen uprimeqtraded=0.6*74.1752*quantity^(-0.4)
gen cprimeqtraded=1.51678*8.23249*quantity^0.51678
gen thetan=bratio*cprimeqtraded/((1-bratio)*uprimeqtraded+bratio*cprimeqtraded)
twoway (hist bratio, percent color(dkorange) graphregion(color(white)))(hist thetan, percent fcolor(none) lcolor(black) lp(longdash)), legend(order(1 "{&theta}{sub:K}" 2 "{&theta}{sub:N}" )) ylab(,nogrid) xscale(lwidth(medium)) yscale(lwidth(medium))
restore
